/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.repository;

import me.zhengjie.business.domain.Snry;
import me.zhengjie.business.domain.vo.SqrjkVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-13
**/
public interface SnryRepository extends JpaRepository<Snry, String>, JpaSpecificationExecutor<Snry> {

    /**
     * 根据身份证查询失能人员
     * @param sfz
     * @return
     */
    Snry queryByBisSnrySfzAndBisSnryUserIdAndSfyx(String sfz, Long userId, String sfyx);

    @Query(value = "select new me.zhengjie.business.domain.vo.SqrjkVo( " +
            "t1.id, " +
            "t1.nickName, " +
            "count(1) as cnt) " +
            "from User t1, Snry t2, Zhsq t3 " +
            " where 1=1 " +
            " and t1.dept.id = '1052' " +
            " and t1.id = t2.bisSnryUserId " +
            " and t2.bisSnryId = t3.bisZhsqSnryId " +
            " group by t1.id, t1.nickName having count(1) >= 3 ")
    Page<SqrjkVo> querySqrjk(String blurry, Pageable pageable);


    @Query(value = "select sum(count(1)) from bis_snry t, sys_user u " +
            " where t.bis_snry_user_id = u.user_id and  " +
            "  t.bis_snry_user_id=?1 and " +
            "  u.is_home is null group by t.bis_snry_sfz",nativeQuery = true)
    Integer checkAddSnry(Long userId);
}